package com.leetcode.no738;

public class Solution {
    public int monotoneIncreasingDigits(int n) {

        String s = String.valueOf(n);

        int len = s.length();
        if (len == 1) {
            return n;
        }

        int right = len - 1;
        int left = len - 2;

        char[] chars = s.toCharArray();

        while (left >= 0) {
//            System.out.println();
            if(chars[left] > chars[right]){
                chars[left] --;
                chars[right] = '9';
            }

            left--;
            right--;
        }

        return Integer.parseInt(new String(chars));
    }

    public static void main(String[] args) {
        Solution s1 = new Solution();
        int res = s1.monotoneIncreasingDigits(10);
        System.out.println(res);
    }
}
